﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SmartRS.com.vn.smartrs.util;
using NHibernate;
using NHibernate.Cfg;
using System.Configuration;
using System.Xml;

namespace SmartRS.com.vn.smartrs.config
{
    public class InitConfig
    {
        public static void readConfig()
        {
            Config.logPath = ConfigurationManager.AppSettings["logPath"];
            Config.dbServer = ConfigurationManager.AppSettings["dbServer"];
            Config.dbName = ConfigurationManager.AppSettings["dbName"];
            Config.dbUser = ConfigurationManager.AppSettings["dbUser"];
            Config.dbPassword = ConfigurationManager.AppSettings["dbPassword"];
            Config.restaurantName = ConfigurationManager.AppSettings["restaurantName"];
            Config.restaurentAddress = ConfigurationManager.AppSettings["restaurentAddress"];
            Config.restaurantPhone = ConfigurationManager.AppSettings["restaurantPhone"];
        }

        public static bool checkConfig()
        {
            if (Config.logPath.Equals("") || Config.dbServer.Equals("") || Config.dbName.Equals("")
            || Config.dbUser.Equals("") || Config.dbPassword.Equals("")
            || Config.restaurantName.Equals("") || Config.restaurentAddress.Equals("") || Config.restaurantPhone.Equals(""))
            {
                return false;
            }
            return true;
        }
        public static bool checkDbConnection(string dbServer, string dbName, string dbUser, string dbPassword)
        {
            try
            {
                var configuration = new NHibernate.Cfg.Configuration();
                configuration.Configure();
                configuration.SetProperty("connection.connection_string", String.Format("Server={0};Database={1};User ID={2};Password={3}", dbServer, dbName, dbUser, dbPassword));
                ISessionFactory sf = configuration.BuildSessionFactory();
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show("Thiết lập cơ sở dữ liệu không đúng!");
                return false;
            }
        }

        public static bool writeConfigDb(string dbServer, string dbName, string dbUser, string dbPassword)
        {
            try
            {
                //Change value config db
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("hibernate.cfg.xml");
                XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
                nm.AddNamespace("HC", "urn:nhibernate-configuration-2.2");
                XmlNode node = xmlDoc.SelectSingleNode("//HC:session-factory/HC:property[@name='connection.connection_string']", nm);

                node.InnerText = String.Format("\r\n      Server={0};Database={1};User ID={2};Password={3};\r\n    ", dbServer, dbName, dbUser, dbPassword);
                xmlDoc.Save("hibernate.cfg.xml");
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show("Ghi thiết lập thất bại!");
                return false;
            }
        }

        public static bool writeAppconfig(string logPathKey, string logPath, string restaurantNameKey, string restaurantName, 
            string restaurentAddressKey, string restaurentAddress, string restaurantPhoneKey, string restaurantPhone)
        {
            try
            {
                System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration.AppSettings.Settings[logPathKey].Value = logPath;
                configuration.AppSettings.Settings[restaurantNameKey].Value = restaurantName;
                configuration.AppSettings.Settings[restaurentAddressKey].Value = restaurentAddress;
                configuration.AppSettings.Settings[restaurantPhoneKey].Value = restaurantPhone;
                configuration.Save();

                ConfigurationManager.RefreshSection("appSettings");
                return true;
            }
            catch(Exception ex)
            {
                MessageBox.Show("Ghi thiết lập ứng dụng xảy ra lỗi!");
                return false;
            }
        }
    }
}